﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Reflection;


namespace csharp窗口句柄z
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        MouseHook mh;

        private void Form1_Load(object sender, EventArgs e)
        {

            //安装鼠标钩子

            mh = new MouseHook();

            mh.SetHook();

            mh.MouseMoveEvent += mh_MouseMoveEvent;

        }

        void mh_MouseMoveEvent(object sender, MouseEventArgs e)
        {   //当前鼠标位置

            int x = e.Location.X;

            int y = e.Location.Y;

            lb_p.Text = string.Format("（{0}，{1}）", x, y);

            int hwnd = Win32Api.WindowFromPoint(x, y);//获取指定坐标处窗口的句柄

            lb_h.Text = hwnd.ToString();

        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {

            mh.UnHook();

        }
    }
}
